﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using DevelopStudio.Basic;
using DevelopStudio.Control.Basic;

namespace DevelopStudio.Control
{
    public partial class WorkingCopyPageControl : PageControl
    {
        public WorkingCopyPageControl()
        {
            InitializeComponent();
        }

        public override void InitializePageControl()
        {
            base.InitializePageControl();

            for(int i = 0; i < this.PageData.WorkingCopies.Count; i++)
            {
                this.listView1.Items.Add(new ListViewItem(new string[] { i.ToString(), this.PageData.WorkingCopies[i].Path }));
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dlg = new FolderBrowserDialog())
            {
                if (dlg.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                string path = Path.GetFullPath(dlg.SelectedPath);

                if (this.PageData.WorkingCopies.Any(a => a.Path == path) == true)
                {
                    MessageBox.Show(Properties.Resources.ErrorWorkingCopyExists);
                    return;
                }

                string url;

                try
                {
                    url = Utility.GetWorkingCopyUrl(path);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(Utility.TraceException(ex));
                    return;
                }

                this.listView1.Items.Add(new ListViewItem(new string[] { this.listView1.Items.Count.ToString(), path }) { Tag = url });
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem var in this.listView1.SelectedItems)
            {
                this.listView1.Items.Remove(var);
            }

            for (int i = 0; i <  this.listView1.Items.Count; i++)
            {
                this.listView1.Items[i].SubItems[0].Text = i.ToString();
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            foreach (ListViewItem var in this.listView1.SelectedItems)
            {
                System.Diagnostics.Process.Start("explorer.exe", var.SubItems[1].Text);
                break;
            }
        }

        public override bool ValidatePage()
        {
            return base.ValidatePage();
        }

        public override void ApplyChanges()
        {
            base.ApplyChanges();

            List<WorkingCopyInfo> copies = this.PageData.WorkingCopies;

            this.PageData.WorkingCopies = new List<WorkingCopyInfo>();

            foreach (ListViewItem var in this.listView1.Items)
            {
                WorkingCopyInfo temp = copies.Find(a => a.Path == var.SubItems[1].Text);

                if (temp == null)
                {
                    temp = new WorkingCopyInfo()
                    {
                        Path = var.SubItems[1].Text,
                        Url = (string)var.Tag
                    };
                }

                this.PageData.WorkingCopies.Add(temp);
            }

            copies.Clear();
        }
    }
}
